﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLKS
{
    public partial class frxoasuanv : Form
    {
        private DataTable _dSNhanVien;

        internal DataTable DSNhanVien
        {
            get { return _dSNhanVien; }
            set { _dSNhanVien = value; }
        }
        public frxoasuanv()
        {
            InitializeComponent();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frxoasuanv_Load(object sender, EventArgs e)
        {
            try
            {
                DSNhanVien = BUS.XuLyNhanVien.TimToanBoNhanVien("nv");

                dgvDSNV.DataSource = DSNhanVien;

                DataRow dr = DSNhanVien.Rows[0];
                txtmanv.Text = dr["MaNhanVien"].ToString();
                txttennv.Text = dr["HoTen"].ToString();
                txtdiachi.Text = dr["DiaChi"].ToString();
                dtpNTNS.Value = DateTime.Parse(dr["NgaySinh"].ToString());
       
            }
            catch (System.Exception )
            {
            	
            }
        }

        private void dgvDSNV_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            try
            {
                DTO.CNHANVIEN nv = new DTO.CNHANVIEN();
                nv.MaNhanVien = int.Parse(txtmanv.Text);
                nv.HoTen = txttennv.Text;
                nv.NgaySinh = dtpNTNS.Value;
                TimeSpan diff = DateTime.Now - dtpNTNS.Value;
                nv.Tuoi = (int)(diff.Days / 365);
                nv.DiaChi = txtdiachi.Text;
                nv.MaLoaiNhanVien = "nv";

                if (!(BUS.XuLyNhanVien.SuaTTNhanVien(nv,nv.MaNhanVien)))
                {
                    MessageBox.Show("Co loi xay ra.Kiem tra file CSDl");
                }
                frxoasuanv_Load(sender, e);
            }
            catch (System.Exception )
            {
                throw new Exception("Sua that bai");
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            try
            {
                DTO.CNHANVIEN nv = new DTO.CNHANVIEN();
                nv.MaNhanVien = int.Parse(txtmanv.Text);

                if (!(BUS.XuLyNhanVien.XoaNhanVien(nv.MaNhanVien))|| !(BUS.XuLyTaiKhoan.XoaTaiKhoan(nv.MaNhanVien)))
                {
                    MessageBox.Show("Co loi xay ra.Kiem tra file CSDl");
                }
                frxoasuanv_Load(sender, e);
            }
            catch (System.Exception )
            {
            	
            }
        }

        private void dgvDSNV_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvDSNV.SelectedRows.Count > 0)
            {
                int index = dgvDSNV.SelectedRows[0].Index;

                DataRow dr = DSNhanVien.Rows[index];
                txtmanv.Text = dr["MaNhanVien"].ToString();
                txttennv.Text = dr["HoTen"].ToString();
                txtdiachi.Text = dr["DiaChi"].ToString();
                dtpNTNS.Value = DateTime.Parse(dr["NgaySinh"].ToString());
            }
        }
    }
}
